﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Incendios0_1.localization
{
    class Sphere
    {
        private Vector3 c;
        private float r;

        public Vector3 C
        {
            get { return c; }
            set { this.c = value; }
        }

        public float R
        {
            get { return r; }
            set { this.r = value; }
        }

        public Sphere(Vector3 c, float R)
        {
            this.c = c;
            this.R = R;
        }

        public static  Vector3[] SecantIntersection(Sphere s, Line l)
        {
            Vector3 G = l.U;
            float a = Vector3.dot(l.V, l.V);
            float b = 2 * Vector3.dot(l.V, G);
            float m = Vector3.dot(G, G);
            float r2 = s.r * s.r;
            float c = m-r2;
            float d = b * b - 4 * a * c;
            if (d < 0)
                return null;
            else
            {
                float p1 = (-b + (float)Math.Sqrt(d)) / 2 * a;
                float p2 = (-b - (float)Math.Sqrt(d)) / 2 * a;
                Vector3[] ps = new Vector3[2];
                ps[0] = l.V * p1 + l.U;
                ps[1] = l.V * p2 + l.U;
                return ps;
            }
        }
    }
}
